﻿<#
.SYNOPSIS  
    Creates Project Server 2010 service applications that will contain the Project Server 2007 web instances to be upgraded. 
.DESCRIPTION  
    
.NOTES
    If the service application already exists, it will be skipped.
.LINK  
    http://7up.codeplex.com 
#>

Write-Host ("   Adding Project Service Applications ({0})..." -f [DateTime]::Now.ToString())

#add SharePoint snap in
if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

#get config
$config = [xml](Get-Content Upgrade.xml)

#add service applications and proxies
foreach($psa in $config.Settings.ServiceApplications.ProjectServiceApplications.ProjectServiceApplication)
{
	if($psa -ne $null)
	{
		#add service app
		$sa = Get-SPServiceApplication | Where-Object {$_.DisplayName -eq $psa.Name}
		if($sa -eq $null)
		{
			Write-Host ("      Adding '{0}' ({1})..." -f $psa.Name, [DateTime]::Now.ToString())
			$sa = New-SPProjectServiceApplication -Name $psa.Name -ApplicationPool $psa.ApplicationPool
		}
		else
		{	
			Write-Host ("      Skipping '{0}' because it already exists ({1})." -f $psa.Name, [DateTime]::Now.ToString())
		}
		
		#add proxy
		$proxyName = ($psa.Name + " Proxy")
		$sap = Get-SPServiceApplicationProxy | Where-Object {$_.DisplayName -eq $proxyName}
		if($sap -eq $null)
		{
			Write-Host ("      Adding '{0}' ({1})..." -f $proxyName, [DateTime]::Now.ToString())
			$proxy = New-SPProjectServiceApplicationProxy -Name ($psa.Name + " Proxy") -ServiceApplication $sa
		}
		else
		{	
			Write-Host ("      Skipping '{0}' because it already exists ({1})." -f $proxyName, [DateTime]::Now.ToString())
		}
	}
}
